home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / inv.cc < prev    next >
C/C++ Source or Header  |  1996-11-03  |  2KB  |  120 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "defun-dld.h"
  28. #include "error.h"
  29. #include "gripes.h"
  30. #include "help.h"
  31. #include "oct-obj.h"
  32. #include "utils.h"
  33.  
  34. DEFUN_DLD (inv, args, ,
  35.   "inv (X): inverse of a square matrix")
  36. {
  37.   octave_value_list retval;
  38.  
  39.   int nargin = args.length ();
  40.  
  41.   if (nargin != 1)
  42.     {
  43.       print_usage ("inv");
  44.       return retval;
  45.     }
  46.  
  47.   octave_value arg = args(0);
  48.  
  49.   int nr = arg.rows ();
  50.   int nc = arg.columns ();
  51.  
  52.   int arg_is_empty = empty_arg ("inverse", nr, nc);
  53.  
  54.   if (arg_is_empty < 0)
  55.     return retval;
  56.   else if (arg_is_empty > 0)
  57.     return Matrix ();
  58.  
  59.   if (nr != nc)
  60.     {
  61.       gripe_square_matrix_required ("inverse");
  62.       return retval;
  63.     }
  64.  
  65.   if (arg.is_real_type ())
  66.     {
  67.       Matrix m = arg.matrix_value ();
  68.  
  69.       if (! error_state)
  70.     {
  71.       int info;
  72.       double rcond = 0.0;
  73.  
  74.       retval = m.inverse (info, rcond, 1);
  75.  
  76.       if (info == -1)
  77.         warning ("inverse: matrix singular to machine precision,\
  78.  rcond = %g", rcond);
  79.     }
  80.     }
  81.   else if (arg.is_complex_type ())
  82.     {
  83.       ComplexMatrix m = arg.complex_matrix_value ();
  84.  
  85.       if (! error_state)
  86.     {
  87.       int info;
  88.       double rcond = 0.0;
  89.  
  90.       retval = m.inverse (info, rcond, 1);
  91.  
  92.       if (info == -1)
  93.         warning ("inverse: matrix singular to machine precision,\
  94.  rcond = %g", rcond);
  95.     }
  96.     }
  97.   else
  98.     {
  99.       gripe_wrong_type_arg ("inv", arg);
  100.     }
  101.  
  102.   return retval;
  103. }
  104.  
  105. // XXX FIXME XXX -- this should really be done with an alias, but
  106. // alias_builtin() won't do the right thing if we are actually using
  107. // dynamic linking.
  108.  
  109. DEFUN_DLD (inverse, args, nargout,
  110.   "inverse (X): inverse of a square matrix")
  111. {
  112.   return Finv (args, nargout);
  113. }
  114.  
  115. /*
  116. ;;; Local Variables: ***
  117. ;;; mode: C++ ***
  118. ;;; End: ***
  119. */
  120.